package com.cx.controller;

import com.cx.pojo.Post;
import com.cx.pojo.Praise;
import com.cx.service.PostService;
import com.cx.service.PraiseService;
import com.cx.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("praise")
public class PraiseController {

    @Autowired
    PraiseService praiseService;
    @Autowired
    PostService postService;
    @Autowired
    UserCenterService userCenterService;



    // 获取帖子的收藏数
    @RequestMapping("selectPostByPostId")
    @ResponseBody
    public Integer selectPostByPostId(String postId){
        List<Praise> praises = praiseService.selectByPostId(Integer.valueOf(postId));
        if (praises.size() == 0){
            return 0;
        }else {
            return praises.size();
        }

    }

    @RequestMapping("selectPostByPostIdAndUserName")
    @ResponseBody
    //是否收藏文章
    public String selectPostByPostId(String postId,String userName){
        System.out.println(postId + "+" + userName);
        Integer id = Integer.valueOf(postId);

        List<Praise> praises = praiseService.selectPostByPostIdAndUserName(id,userName);
        if (praises.isEmpty()){
            System.out.println("yes");
            return "yes";
        }else {
            System.out.println("no");
            return "no";
        }

    }

    // 某用户收藏的帖子
    @RequestMapping("selectPostByUserName")
    @ResponseBody
    public List<Post> selectPostByUserName(String userName){
        List<Praise> praises = praiseService.selectPostByUserName(userName);
        List<Post> posts = new ArrayList<>();
//        List<Post> posts1 = postService.selectByPostId(praises.get(0).getPostId());
        for (int i = 0; i < praises.size(); i++){
            // 将查出来的praises集合内的postId去在post表内查数据，然后插入空的集合内
            posts.add(postService.selectByPostId(praises.get(i).getPostId()).get(0));
        }
        return posts;
    }

    @RequestMapping("updatePost")
    @ResponseBody
    public String updatePost(Praise praise){
        Integer praiseId = praise.getPraiseId();
        Integer rows = praiseService.updatePost(praiseId,praise);
        if (rows > 0){
            return "success";
        }else {
            return "fail";
        }
    }

    @RequestMapping("deletePraise")
    @ResponseBody
    public String deletePraise(Integer praiseId){
        Integer rows = praiseService.deletePraise(praiseId);
        if (rows > 0){
//            Integer row = postService.updatePostCelDelOne()
            return "success";
        }else {
            return "fail";
        }
    }

    @RequestMapping("deletePraiseByOther1")
    @ResponseBody
    public String deletePraiseByOther1(String userName,String postId){
        System.out.println(userName + postId);
//        String a = userName;
        Integer id = Integer.valueOf(postId);
        Integer rows = praiseService.deletePraiseByOther1(id,userName);
        System.out.println(rows);
        if (rows > 0){
            Integer row = postService.updatePostCelDelOne(id);
            System.out.println(row);
            if (row > 0){
                return "success";
            }else {
                return "fail";
            }

        }else {
            return "fail";
        }
    }

    @RequestMapping("insertPraise")
    @ResponseBody
    public String insertPraise(String userName,String postId){
        System.out.println(userName + postId);
        Praise praise = new Praise();
        praise.setPostId(Integer.valueOf(postId));
        praise.setOther1(userName);
        Integer rows = praiseService.insertPraise(praise);
        if (rows > 0){
            Integer row = postService.updatePostCelAddOne(Integer.valueOf(postId));
            if (row > 0){
                return "success";
            }else {
                return "fail";
            }
        }else {
            return "fail";
        }
    }


//    @RequestMapping("insertPraise")
//    @ResponseBody
//    public String insertPraise(String userName,String postId){
//        System.out.println(userName + postId);
//        Praise praise = new Praise();
//        praise.setPostId(Integer.valueOf(postId));
//        praise.setOther1(userName);
//        Integer rows = praiseService.insertPraise(praise);
//        if (rows > 0){
//            return "success";
//        }else {
//            return "fail";
//        }
//    }

}
